import axios from 'axios'

export const getSubtoken = ({state, commit, rootState}) => {
  const apiRoot = rootState.api.apiRoot
  setAxiosHeaders(state.token)
  return axios.get(
    apiRoot + 'subtokens/' + state.token,
    { headers: { 'Authorization': state.token } })
    .then(
      function (response) {
        commit('setValid', true)
      })
    .catch(
      function (error) {
        console.log(error)
        commit('setValid', false)
        setAxiosHeaders('')
      })
}

const setAxiosHeaders = (token) => {
  axios.defaults.headers.common['Authorization'] = token
}
